package view;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import controlador.Tragamonedas;

import clases.casillas.Casilla;
import java.awt.Font;
import java.util.ArrayList;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PanelAgregarCasillaARanura extends JPanel implements FocusListener,ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox<Integer> comboBox_Ranura;
	private JComboBox<String> comboBox_CasillasDisponibles;
	private Tragamonedas sys;
	private JLabel lblAgregarCasillaA;
	private JLabel lblCasillasDisponibles;
	private JButton btnAgregar;
	private JLabel lblRanura;


	public PanelAgregarCasillaARanura(Tragamonedas sys) {
		setLayout(null);
		this.sys = sys;

		btnAgregar = new JButton("Agregar Casilla");
		btnAgregar.addActionListener(this);
		

		btnAgregar.setBounds(98, 219, 148, 23);
		add(btnAgregar);

		comboBox_CasillasDisponibles = new JComboBox<String>();
		comboBox_CasillasDisponibles.setBounds(189, 155, 148, 22);
		add(comboBox_CasillasDisponibles);

		ArrayList<Casilla> tempCasillas = this.sys.getCasillas();
		for (Casilla casilla : tempCasillas) {
			comboBox_CasillasDisponibles.addItem(casilla.getNombre());
		}

		lblRanura = new JLabel("Ranura:");
		lblRanura.setBounds(27, 77, 137, 14);
		add(lblRanura);

		comboBox_Ranura = new JComboBox<Integer>();
		comboBox_Ranura.addFocusListener(this);
		comboBox_Ranura.setBounds(189, 69, 148, 22);
		add(comboBox_Ranura);

		int tempInt = this.sys.getCantidadRanuras();

		for (int i = 0; i < tempInt; i++) {
			this.comboBox_Ranura.addItem(i);
		}
		
		lblAgregarCasillaA = new JLabel("Agregar Casilla a Ranura");
		lblAgregarCasillaA.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblAgregarCasillaA.setBounds(98, 6, 175, 14);
		add(lblAgregarCasillaA);

		lblCasillasDisponibles = new JLabel("Casillas Disponibles:");
		lblCasillasDisponibles.setBounds(23, 159, 141, 14);
		add(lblCasillasDisponibles);

	}

	@Override
	public void focusGained(FocusEvent event) {
		if (event.getSource() == this.comboBox_Ranura) {
			comboBox_Ranura.removeAllItems();
			this.repaint();
			
			int tempInt2 = sys.getCantidadRanuras();

			for (int i = 0; i < tempInt2; i++) {
				this.comboBox_Ranura.addItem(i);
			}

		}

	}

	@Override
	public void focusLost(FocusEvent event) {
		
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == btnAgregar){
			if(sys.AsignarCasillaARanura((Integer)comboBox_Ranura.getSelectedItem(), (String)comboBox_CasillasDisponibles.getSelectedItem())){

				JOptionPane.showMessageDialog(null,
							"Se ha asignado la casilla a la ranura: " + (Integer)comboBox_Ranura.getSelectedItem() , "Informacion",
							JOptionPane.OK_OPTION);

			}else{
				JOptionPane.showMessageDialog(null,
						"No se ha asignado la casilla a la ranura" , "Advertencia",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}
}
